//
//  MosViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/7/10.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "MosViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "IWTextView.h"
#import "PersonalDataVCtrol.h"
#import "IGGetMyinfoApi.h"
#import"IGUsersynopsisApi.h"
#import "IGUpdateContentApi.h"


@interface MosViewController () <UITableViewDataSource, UITableViewDelegate> {
//    UserModel *userModel;
    IWTextView *textView ;
}

@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *table;
@property(nonatomic,strong)IGUsersynopsisApi *synopsisApi;
@property(nonatomic,strong)IGUpdateContentApi *contentApi;

@end

@implementation MosViewController
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = @"简介";
    self.title = NSLocalizedString(@"Intro", nil);
    self.table.backgroundColor=[UIColor c2Color];
    if (@available(iOS 11.0, *)) {
        self.table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.table.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
        self.table.scrollIndicatorInsets = self.table.contentInset;
    }
//    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sure:)];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Confirm", nil) style:UIBarButtonItemStylePlain target:self action:@selector(sure:)];
    [right setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor  c1Color]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextViewTextDidChangeNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    [textView resignFirstResponder];
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
    [self.view endEditing:YES];
}

#pragma mark — UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    CGFloat x = cell.separatorInset.left;
    cell.backgroundColor=[UIColor c2Color];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    textView = [[IWTextView alloc] initWithFrame:CGRectMake(x, 15, KSCREENWIDTH-2*x, KSCREENHEIGHT - 200)];
    textView.font = [UIFont systemFontOfSize:14];
    textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textView.layer.borderWidth = 1;
    textView.tag = indexPath.row;
//    textView.placeholder = @"这里记录着你的辉煌与过往";
    textView.placeholder = NSLocalizedString(@"Your Glorious Time", nil);
    textView.text = self.context;
    textView.keyboardType = UIKeyboardTypeDefault;
    [cell.contentView addSubview:textView];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KSCREENHEIGHT - 25 - 64;
}

- (void)textChange:(NSNotification *)not {
    if ([not.object isMemberOfClass:[IWTextView class]]) {
        textView = not.object;
        if (textView.tag == 0) {
            UITextRange *markedRange = [textView markedTextRange];
            if (markedRange) {
                return;
            }else
            if (textView.text.length >500) {
//                [self presentMessageTips:@"最多可输入500个字"];
                [self presentMessageTips:NSLocalizedString(@"Input 500 Words Max.", nil)];
                NSRange range = [textView.text rangeOfComposedCharacterSequenceAtIndex:1500];
                textView.text = [textView.text substringToIndex:range.location];
            }
            self.context = textView.text;
        }
    }
}

- (void)sure:(UIBarButtonItem *)btn {
    
        [self.view endEditing:YES];
        [textView resignFirstResponder];

    if (self.context.length == 0) {
//        [self presentMessageTips:@"请输入简介内容"];
        [self presentMessageTips:NSLocalizedString(@"Complete the profile", nil)];
        return;
    }
    BOOL ishas = [NSString stringContainsEmoji:self.context];
    NSLog(@" %@ 表情", ishas?@"有":@"没有");
    if (ishas)
    {
//        [self presentMessageTips:@"不支持表情符号"];
        [self presentMessageTips:NSLocalizedString(@"Emoji Unavailable", nil)];
        return;
    }
    self.contentApi=[[IGUpdateContentApi alloc]initWithUid:[IGUser getUid] content:self.context];
    [self.contentApi startWithDecodeClass:[IGUpdateContentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGUpdateContentResp *resp=responseObject;
            IGUpdateContentModel *model=resp.data;
//            [self.table reloadData];
//            [self.view endEditing:YES];
//            [textView resignFirstResponder];
              [UIViewController ig_popViewController:[PersonalDataVCtrol class]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self presentMessageTips:model.msg];
            });
            }
    }];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.table endEditing:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
